package com.sx.controller;

import com.sx.basic.HttpConst;
import com.sx.basic.ResponseEntity;
import com.sx.entity.system.AdminEntity;
import com.sx.entity.system.MenuEntity;
import com.sx.entity.system.RoleEntity;
import com.sx.mapping.AdminMapping;
import com.sx.service.system.IAdminService;
import com.sx.service.system.IMenuService;
import com.sx.service.system.IRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author AdinZ
 * @date 2020/11/9 15:44
 */
@Api("管理员操作接口")
@RestController
public class AdminController {

    @Resource private IAdminService adminService;
    @Resource private IRoleService iRoleService;
    @Resource private IMenuService menuService;

    /* ===============================管理员账户操作====================================*/
    @PostMapping(AdminMapping.LOGIN) @ApiOperation("管理后台登陆")
    public ResponseEntity<String> login(@RequestParam("username")String username,
                                        @RequestParam("password")String password){
        return this.adminService.login(username,password);
    }
    @GetMapping(AdminMapping.LOGOUT) @ApiOperation("管理员退出登陆")
    public ResponseEntity logout(@RequestHeader(HttpConst.TOKEN)String token){
        return this.adminService.logout(token);
    }
    @GetMapping(AdminMapping.GET_INFO) @ApiOperation("获取登陆管理员基本资料")
    public ResponseEntity getInfo(@RequestHeader(HttpConst.TOKEN)String token){
        return this.adminService.getInfo(token);
    }
    @GetMapping(AdminMapping.ADMINS) @ApiOperation("系统管理员列表")
    public ResponseEntity<List<AdminEntity>> adminGet(@RequestParam("page")int page,
                                   @RequestParam("size")int size){
        return this.adminService.findPage(page, size);
    }
    @GetMapping("/admin/{id}")@ApiOperation("获取单个管理资料")
    public ResponseEntity<AdminEntity> adminGet(@PathVariable("id")Long id){
        AdminEntity entity = this.adminService.get(id);
        return new ResponseEntity<AdminEntity>().toSuccess(entity);
    }

    @PostMapping(AdminMapping.ADMINS) @ApiOperation("修改管理员密码")
    public ResponseEntity adminPost(@RequestHeader(HttpConst.TOKEN)String token,
                                    @RequestParam("oldPassword")String oldPassword,
                                    @RequestParam("password")String password){

        return this.adminService.updatePwd(token,oldPassword, password);
    }

    @PutMapping(AdminMapping.ADMINS) @ApiOperation("添加/更新管理员资料")
    public ResponseEntity adminPut(@RequestBody AdminEntity entity){
        return this.adminService.putAdmin(entity);
    }
    @DeleteMapping(AdminMapping.ADMINS)@ApiOperation("删除管理员")
    public ResponseEntity adminDelete(@RequestParam("id")Long[] ids){
        return this.adminService.deleteAdmin(ids);
    }



    /* ===============================管理员角色操作====================================*/
    @GetMapping(AdminMapping.ROLE) @ApiOperation("角色列表")
    public ResponseEntity<List<RoleEntity>> roleGet(@RequestParam("page")int page,
                                  @RequestParam("size")int size){
        return this.iRoleService.findPage(page,size);
    }

    @GetMapping("/admin/role/{id}")@ApiOperation("获取单个管理资料")
    public ResponseEntity<RoleEntity> roleGet(@PathVariable("id")Long id){
        return new ResponseEntity<>().toSuccess(this.iRoleService.get(id));
    }

    @PutMapping(AdminMapping.ROLE)@ApiOperation("添加修改角色")
    public ResponseEntity rolePut(@RequestBody RoleEntity entity){
        return this.iRoleService.rolePut(entity);
    }

    @DeleteMapping(AdminMapping.ROLE)@ApiOperation("删除角色")
    public ResponseEntity roleDelete(@RequestParam("id")Long[] ids){
        this.iRoleService.delete(ids);
        return new ResponseEntity().toSuccess();
    }

    /* ===============================管理员后台菜单操作====================================*/
    @GetMapping(AdminMapping.MENU)@ApiOperation(value = "获取菜单数据",notes = "action 为空时是全部列表, tree时 是树状列表")
    public ResponseEntity<List<MenuEntity>> menuGet(@RequestParam(value = "action",required = false)@ApiParam(value = "数据属性",allowableValues = "[ ,tree]")String action){
        return this.menuService.getList(action);
    }





}
